<?php

function route_class()
{
    return str_replace('.', '-', Route::currentRouteName());
}

function make_excerpt($value, $length = 200)
{
    $excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
    return str_limit($excerpt, $length);
}

function model_link($value, $model, $prefix = '')
{
    //获取数据模型的复数蛇形命名
    $model_name =  model_plural_name($model);

    //初始化前缀
    $prefix = $prefix ? "/$prefix/" : '/';

    //使用站点URL拼接全量URL
    $url = config('app.url') . $prefix . $model_name . '/' . $model->id;

    //拼接HTML a标签, 并返回
    return '<a href="' . $url . '" target="_blank">' . $value . '</a>'; 
}

function model_admin_link($value, $model)
{
    return model_link($value, $model, 'admin');
}

function model_plural_name($model)
{
    //从实体中获取完整类名,例如:APP\MOdels\User
    $full_class_name = get_class($model);

    //获取基础类名, 例如:传参'App\Models\User' 会得到User
    $class_name = class_basename($full_class_name);

    //蛇形命名, 例如: 传参'User',会得到'user' , 'FooBar'会得到'foo_bar'
    $snake_case_name = snake_case($class_name);

    //获取子串的复数形式,例如: 传参'user' 得到'users'
    return str_plural($snake_case_name);
}
